package com.whfc.fuum.redis.impl;

import com.whfc.common.enums.SysRulePlatform;
import com.whfc.common.util.JSONUtil;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.redis.SysUserRedisDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-06-19 15:24
 */
@Service
public class SysUserRedisDaoImpl implements SysUserRedisDao {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String SYS_USER_RULES = "sys_user_rules";

    private static final String SYS_USER_DEPT_IDS = "sys_user_dept_ids";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setUserRules(Integer userId, Integer platform, List<SysRuleDTO> ruleDTOS) {
        logger.info("更新用户权限缓存,userId:{},rules:{}", userId, ruleDTOS.size());
        String hashKey = userId + "&" + platform;
        String val = JSONUtil.toString(ruleDTOS);
        redisTemplate.opsForHash().put(SYS_USER_RULES, hashKey, val);
    }

    @Override
    public List<SysRuleDTO> getUserRules(Integer userId, Integer platform) {
        List<SysRuleDTO> ruleList = null;
        String hashKey = userId + "&" + platform;
        String val = String.valueOf(redisTemplate.opsForHash().get(SYS_USER_RULES, hashKey));
        if (!StringUtils.isEmpty(val)) {
            ruleList = JSONUtil.parseArray(val, SysRuleDTO.class);
        }
        logger.info("查询用户权限缓存,userId:{},rules:{}", userId, ruleList == null ? 0 : ruleList.size());
        return ruleList;
    }

    @Override
    public void deleteUserRules(Integer userId) {
        //清除MS权限
        String msHashKey = userId + "&" + SysRulePlatform.MS.getValue();
        redisTemplate.opsForHash().delete(SYS_USER_RULES, msHashKey);
        logger.info("删除用户MS权限缓存,userId:{}, platform:1", userId);
        //清除MP权限
        String mpHashKey = userId + "&" + SysRulePlatform.MP.getValue();
        redisTemplate.opsForHash().delete(SYS_USER_RULES, mpHashKey);
        logger.info("删除用户权限缓存,userId:{}, platform:2", userId);
    }

    @Override
    public void deleteUserRules(List<Integer> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return;
        }
        List<String> hashKeys = new ArrayList<>();
        for (Integer userId : userIds) {
            //删除MS权限
            hashKeys.add(userId + "&" + SysRulePlatform.MS.getValue());
            //删除MP权限
            hashKeys.add(userId + "&" + SysRulePlatform.MP.getValue());
        }

        redisTemplate.opsForHash().delete(SYS_USER_RULES, hashKeys.toArray());
        logger.info("删除用户权限缓存,userIds:{}", userIds);
    }

    @Override
    public void setDeptIds(Integer userId, List<Integer> deptIdList) {
        logger.info("更新用户组织机构ID缓存,userId:{},deptIdList:{}", userId, deptIdList.size());
        String hashKey = String.valueOf(userId);
        String val = JSONUtil.toString(deptIdList);
        redisTemplate.opsForHash().put(SYS_USER_DEPT_IDS, hashKey, val);
    }

    @Override
    public List<Integer> getDeptIds(Integer userId) {
        List<Integer> deptIdList = null;
        String hashKey = String.valueOf(userId);
        String val = String.valueOf(redisTemplate.opsForHash().get(SYS_USER_DEPT_IDS, hashKey));
        if (!StringUtils.isEmpty(val)) {
            deptIdList = JSONUtil.parseArray(val, Integer.class);
        }
        logger.info("查询用户组织机构ID缓存,userId:{},deptIdList:{}", userId, deptIdList == null ? 0 : deptIdList.size());
        return deptIdList;
    }

    @Override
    public void deleteUserDeptIds(Integer userId) {
        String hashKey = String.valueOf(userId);
        redisTemplate.opsForHash().delete(SYS_USER_DEPT_IDS, hashKey);
        logger.info("删除用户组织机构ID缓存,userId:{}", userId);
    }


    @Override
    public void deleteUserDeptIds(List<Integer> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return;
        }
        List<String> hashKeys = new ArrayList<>();
        for (Integer userId : userIds) {
            hashKeys.add(String.valueOf(userId));
        }

        redisTemplate.opsForHash().delete(SYS_USER_DEPT_IDS, hashKeys.toArray());
        logger.info("删除用户组织机构ID列表缓存,userIds:{}", userIds);
    }

    @Override
    public void deleteUserDeptIds() {
        logger.info("删除所有用户组织机构关系");
        redisTemplate.delete(SYS_USER_DEPT_IDS);
    }
}
